package com.sat.rest.papeletas.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.sat.rest.papeletas.domain.Papeleta;

@Service("papeletaService")
public class PapeletaService {
	protected static Logger logger = Logger.getLogger("service");
	SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
	// In-memory list
	private List<Papeleta> papeletas = new ArrayList<Papeleta>();

	public PapeletaService() {
		logger.debug("Init database");
		// Create in-memory list
		try {
			{
				Papeleta papeleta;
				papeleta = new Papeleta("10664149", "8682296", "Q10664148",
						"G02", "COX292", "Graves", formato.parse("11/06/2010"));
				papeletas.add(papeleta);
			}
			{
				Papeleta papeleta;
				papeleta = new Papeleta("10664149", "7338720", "Q10664148",
						"B09", "COX292", "Muy Graves",
						formato.parse("12/12/2007"));
				papeletas.add(papeleta);
			}
			{
				Papeleta papeleta;
				papeleta = new Papeleta("10664149", "6986716", "Q10664148",
						"C28", "COX292", "Graves", formato.parse("02/05/2007"));
				papeletas.add(papeleta);
			}
			{
				Papeleta papeleta;
				papeleta = new Papeleta("31664149", "6986716", "Q10664148",
						"C28", "COX292", "Graves", formato.parse("02/05/2007"));
				papeletas.add(papeleta);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Retrieves all persons
	 */
	public List<Papeleta> getAll() {
		logger.debug("Retrieving all papeletas");

		return papeletas;
	}

	/**
	 * @param dni
	 * @return
	 */
	public List<Papeleta> obtenerSanciones(String dni) {
		List<Papeleta> sancionesBuscadas = new ArrayList<Papeleta>();
		for (Papeleta sancionInfo : papeletas) {
			if (sancionInfo.getDni().equals(dni)) {
				sancionesBuscadas.add(sancionInfo);
			}
		}
		if (sancionesBuscadas.size() > 0) {
			return sancionesBuscadas;
		} else {
			logger.debug("No records found");
			return null;
		}

	}

}
